;; -*- lexical-binding: t -*-

;; MS-Windows Settings.

(set-locale-environment "zh_CN.UTF-8")

(setq-default buffer-file-coding-system 'utf-8-unix)

(ignore-errors
  (when (and 
	 (require 'cygwin-mount)
	 (featurep 'cygwin-mount))
    (setq cygwin-mount-cygwin-bin-directory "C:/msys64/usr/bin/")
    (cygwin-mount-activate)))

(setq-default process-coding-system-alist
	      '(("[pP][lL][iI][nN][kK]" gbk-dos . gbk-dos)
		("[pP][iI][nN][gG]" gbk-dos . gbk-dos)
		("[cC][mM][dD][pP][rR][oO][xX][yY]" gbk-dos . gbk-dos)
		("[gG][sS]" gbk-dos . gbk-dos)
		("[fF][iI][nN][dD]" gbk-dos . gbk-dos)
		("[gG][rR][eE][pP]" gbk-dos . gbk-dos)
		("[gG][sS][wW][iI][nN]32[cC]" gbk-dos . gbk-dos)))

(setq TeX-print-command "start \"\" %f")
(setq auto-save-file-name-transforms
      '(("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'" "z:/TEMP/\\2" t)
	("/[^/]*:\\([^/]*/\\)*\\([^/]*\\)" "z:/TEMP/\\2" t)))
;; (setq TeX-shell "bash")
;; (setq doc-view-ghostscript-program "gswin64c.exe")
(setq ess-directory-containing-R "C:")
(require 'ess-r-mode)
(setenv "R_HOME"
	(car (ess-find-rterm
	      (concat ess-directory-containing-R "/R/") "")))

(setq-default inferior-ess-r-program
	      (car (ess-find-rterm
	     	    (concat ess-directory-containing-R "/R/") "bin/x64/Rterm.exe")))

;; (setenv "PATH"
;; 	(concat
;; 	 (getenv "PATH") ";"
;; 	 (replace-regexp-in-string
;; 	  "/" "\\\\"
;; 	  (car
;; 	   (ess-find-rterm
;; 	    (concat ess-directory-containing-R "/R/") "bin")))))

;; (setenv "PATH"
;; 	(concat
;; 	 (getenv "PATH") ";"
;; 	 (replace-regexp-in-string
;; 	  "/" "\\\\"
;; 	  (car
;; 	   (ess-find-rterm
;; 	    (concat ess-directory-containing-R "/R/") "bin/x64")))))

;; (setq explicit-bash-args (quote ("--noediting" "-i")))

(setenv "PATH" "C:\\msys64\\mingw64\\bin;C:\\msys64\\mingw64\\opt\\bin;C:\\msys64\\usr\\bin;C:\\Program Files\\7-Zip-Zstandard;E:\\autodock;C:\\nodejs;C:\\Users\\Kel\\AppData\\Roaming\\npm;C:\\Users\\Kel\\AppData\\Local\\Pandoc;C:\\OpenBabel;C:\\Windows\\System32;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0")
;; C:\\gs\\gs9.26\\bin;

(setenv "MSYSTEM" "MINGW64")
;; (setenv "TERM" "cygwin")
;; (setenv "GNUTERM" "windows")

(setq gnutls-trustfiles '("C:/msys64/usr/ssl/certs/ca-bundle.crt"))
;; (setq langtool-language-tool-jar "C:/LanguageTool-3.3/languagetool-commandline.jar")

;; Print support.
(setq-default ps-lpr-command (expand-file-name "C:/Program Files/Ghostgum/gsview/gsprint.exe"))
(setq-default ps-printer-name t)
(setq-default ps-printer-name-option nil)
(setq ps-lpr-switches '("-query")) ; show printer dialog

(setq python-shell-interpreter "C:/Python27/Scripts/ipython.exe")
(setq python-shell-interpreter-args "-i")
(setq semantic-c-dependency-system-include-path
      '("/mingw64/x86_64-w64-mingw32/include" "/mingw64/include"))

;; (setq tramp-auto-save-directory "/tmp")

;; (eval-when-compile
;;   (require 'tramp)
;;   (eval-after-load "tramp"
;;     (progn
;;       (connection-local-set-profile-variables
;;        'remote-bash
;;        '((explicit-shell-file-name . "/bin/bash")
;; 	 (explicit-bash-args . ("-i"))))
;;       (connection-local-set-profiles
;;        '(:application tramp)
;;        'remote-bash))))

(setq woman-manpath
      '("C:/msys64/usr/share/man"
	("/bin" . "/usr/share/man")
	("/usr/bin" . "/usr/share/man")
	("/sbin" . "/usr/share/man")
	("/usr/sbin" . "/usr/share/man")
	;; ("/usr/local/bin" . "/usr/local/man")
	;; ("/usr/local/bin" . "/usr/local/share/man")
	;; ("/usr/local/sbin" . "/usr/local/man")
	;; ("/usr/local/sbin" . "/usr/local/share/man")
	;; ("/usr/X11R6/bin" . "/usr/X11R6/man")
	;; ("/usr/bin/X11" . "/usr/X11R6/man")
	;; ("/usr/games" . "/usr/share/man")
	("/opt/bin" . "/opt/man")
	("/opt/sbin" . "/opt/man")
	("/mingw64/bin" . "/mingw64/share/man")
	"C:/msys64/mingw64/share/man"
	))

(when (and
       (featurep 'tex-site)
       (featurep 'tex-mik)
       (featurep 'texmathp))
  (require 'tex-site)
  (require 'tex-mik)
  (require 'texmathp))

(mapc (lambda (mode-hook)
	(add-hook mode-hook
		  (lambda ()
		     (set-buffer-process-coding-system
		      'utf-8-unix 'utf-8-unix))))
      '(inferior-js-mode-hook))

;; (dired-async-mode 1)

(setq preview-gs-command "gswin64c.exe")
;; (setq doc-view-ghostscript-program "gswin64c.exe")

(setq doc-view-ghostscript-options
      '("-dSAFER" "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4" "-dBATCH" "-dGraphicsAlphaBits=4" "-sFONTPATH=\"C:\\Windows\\Fonts\"" "-I C:/gs/gs9.26/lib" "-dQUIET"))

;; Stop backup tramp'd files.
;; (add-to-list 'backup-directory-alist
;;              (cons tramp-file-name-regexp nil))

;; (advice-add
;;  'tramp-sh-handle-expand-file-name :around
;;  (lambda (orig-func name &optional dir)
;;    (if (save-match-data (string-match "^[a-z]+:[/\\]" name))
;;        (expand-file-name name (file-name-directory name))
;;      (apply orig-func (list name dir)))))

;; (defun grep-use-cmdproxy (original &rest args)
;;   (let ((shell-file-name (executable-find "cmdproxy")))
;;     (apply original args)))

;; (with-eval-after-load 'grep
;;   (advice-add 'lgrep :around #'grep-use-cmdproxy)
;;   (advice-add 'rgrep :around #'grep-use-cmdproxy))

(eval-when-compile
  (require 'grep)
  (eval-after-load "grep"
    '(progn
       (grep-apply-setting 'grep-command "grep  -nH -r ."))))

(setq pdf-info-epdfinfo-program "C:/msys64/mingw64/bin/epdfinfo.exe")

;; For bug.
;; (require 'epa-file)
(custom-set-variables '(epg-gpg-program  "/usr/bin/gpg")
		      '(package-gnupghome-dir "/home/Kel/.emacs.d/elpa/gnupg"))
;; (epa-file-enable)

;; (require 'w32-browser)

(w32-register-hot-key [s-s])
